package 剑指Offer.easy;

import java.util.Deque;
import java.util.LinkedList;

public class 二叉树的深度 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    class Solution {
        public int maxDepth(TreeNode root) {
            if (root == null) return 0;
            Deque<TreeNode> deque = new LinkedList<>();
            deque.addLast(root);
            int count = 1;
            while (!deque.isEmpty()) {
                int size = deque.size();
                for (int i = 0; i < size; i++) {
                    TreeNode treeNode = deque.pollFirst();
                    if (treeNode.left != null) deque.addLast(treeNode.left);
                    if (treeNode.right != null) deque.addLast(treeNode.right);
                }
                count++;
            }
            return count - 1;
        }
    }
}
